import string
import tile
import random

class TileManager:
    #grabs the information on tiles from a database, sets up tiles, etc
    
    def __init__(self):
        self.tiles = self.pullTilesFromDoc("tiles.txt")

    #pulls the tiles from the text document and parses it out
    #@param file: the name of the file being pulled from
    #@return: array of all tiles
    def pullTilesFromDoc(self, file):
        f = open(file, 'r')
        arr = []
        i = 0
        for line in f:
            if(line[0] != '#'):
                temp = line.split('||')
                arr.append(tile.Tile(int(temp[0]), temp[1], temp[2].upper(), temp[3], temp[4], int(temp[5]), int(temp[6]), int(temp[7]), int(temp[8]), int(temp[9]), int(temp[10]), temp[11], temp[12], int(temp[13]), temp[14].rstrip()))
                i = i + 1
        return arr
            
    
    #gets and returns a tile
    #@param num: the id number of the tile
    def getTile(self, num):
        return self.tiles[num]

    #returns an array of 3 arrays: the A B and C stack
    #@param num: the number of players in the game
    def createTileLists(self, num):
        retArr = []
        
        A = []
        B = []
        C = []
        
        for tile in self.tiles:
            if (tile.getStack() == 'A'):
                A.append(tile)
            elif (tile.getStack() == 'B'):
                B.append(tile)
            else:
                C.append(tile)
                
        random.shuffle(A)
        random.shuffle(B)
        random.shuffle(C)
        
        retArr = A[:15] + B[:15] + C
        
        return retArr


